


attach(COVID_Data_June_27_Treatments)


###### Q22

# prepare data
data9 <- corpus(COVID_Data_June_27_Treatments, text_field = 'Q22')

docvars(data9)$text <- texts(data9)
data9 <- dfm(data9, stem = TRUE, remove = c(stopwords(source = "smart"),"pandemic", "virus", "respond", "spread"),
             remove_punct = TRUE) %>% dfm_trim(min_termfreq = 25)

out9<-convert(data9, to = 'stm')

summary(out9)

out9$meta$Negative <- as.factor(out9$meta$Negative)
out9$meta$Chinese <- as.factor(out9$meta$Chinese)


##############How many topics? ##########################################

set.seed(5)
kResult9 <- searchK(out9$documents, out9$vocab, K = c(5,7, 10),
                    init.type = "Spectral", prevalence =~ Negative*Chinese, data = out9$meta)

plot(kResult9)
par(mfrow = c(1, 1),mar = c(5, 5, 5, 5))
plot(kResult9$results$semcoh, kResult9$results$exclus, xlab = "Semantic Coherence",
     ylab = "Exclusivity")

text(kResult9$results$semcoh, kResult9$results$exclus, labels = paste("K",
                                                                      kResult9$results$K), pos = 1)

knitr::kable(kResult9$results)


#fit models and effect estimates


data_10 <- stm(documents = out9$documents,
               vocab = out9$vocab,
               data = out9$meta,
               prevalence = ~ Chinese* Negative,
               K = 10, verbose = FALSE)
prep_10 <- estimateEffect(1:10 ~ Chinese*Negative, data_10,
                          meta = out9$meta)

labelTopics(data_10, topics = NULL, n = 10)

par(mfrow = c(1, 1),mar = c(7, 1,3, .5))
plot(data_10, xlim=c(0,.7), n = 7, labeltype = "frex", topic.names = c("(1) Trump's Response", "(2) Federal Government Response", "(3) Leadership's Response (General)", "(4) COVID-19 Testing", "(5) Public's Response", "(6) Citizen's Response", "(7) President's Response", "(8) Bipartisan Response", "(9) Governor's Response", "(10) Government's Response (Not Trump)"), custom.labels = "", text.cex = .7)

summary(prep_10)


thoughts3<- findThoughts(data_10, texts = out9$meta$Q22, topics =9 , n = 15)

head(thoughts3)


###################### PLOTS #################################



plot(prep_10, covariate="Chinese", topics=c(3), model=data_10, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     moderator = "Negative", moderator.value = 0,
     xlab="", main="Positively Framed \n Performance Information",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.0,.05), labeltype ="custom", custom.labels=c('Chinese Virus', 'COVID-19'))


plot(prep_10, covariate="Chinese", topics=c(3), model=data_10, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     moderator = "Negative", moderator.value = 1,
     xlab="", main="Negatively Framed \n Performance Information",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.0,.05), labeltype ="custom", custom.labels=c('Chinese Virus', 'COVID-19'))

mtext("The Conditional Effect of Chinese Virus on Blaming Leadership's Response \n (B = -0.02, SE = 0.01, p = 0.002)", outer = TRUE, cex = .8)




####################Find thoughts

attach(COVID_Data_June_27_Treatments)

data9r <- corpus(COVID_Data_June_27_Treatments, text_field = 'Q22')

docvars(data9r)$text <- texts(data9r)
data9r <- dfm(data9r, stem = TRUE, remove = c(stopwords(source = "smart"),"pandemic", "virus", "respond",  "mask", "wear", "spread"),
              remove_punct = TRUE) %>% dfm_trim(min_termfreq = 25)

out<-convert(data9r, to = 'stm')


model <- stm(out$documents, out$vocab, K = 10, max.em.its = 160, data = out$meta,
             init.type = "Spectral", prevalence =~ Negative*Chinese)

summary(model)


thoughts2<- findThoughts(model, texts = out$meta$Q22, topics = 10, n = 25)

head(thoughts2)



























############Conservative Only



attach(COVID_Data_June_27_Treatments)

COVID_Data_June_27_Treatments_Conservative <- COVID_Data_June_27_Treatments[ which(Conservative=='1'),]




###### Q22

# prepare data
data9r <- corpus(COVID_Data_June_27_Treatments_Conservative, text_field = 'Q22')

docvars(data9r)$text <- texts(data9r)
data9r <- dfm(data9r, stem = TRUE, remove = c(stopwords(source = "smart"),"pandemic", "virus", "respond",  "mask", "wear", "spread"),
              remove_punct = TRUE) %>% dfm_trim(min_termfreq = 25)

out9r<-convert(data9r, to = 'stm')


##############How many topics? ##########################################

set.seed(5)
kResult9r <- searchK(out9r$documents, out9r$vocab, K = c(5,7, 10),
                     init.type = "Spectral", prevalence =~ Negative, data = out9r$meta)

plot(kResult9r)
par(mfrow = c(1, 1),mar = c(5, 5, 5, 5))
plot(kResult9r$results$semcoh, kResult9r$results$exclus, xlab = "Semantic Coherence",
     ylab = "Exclusivity")

text(kResult9r$results$semcoh, kResult9r$results$exclus, labels = paste("K",
                                                                        kResult9r$results$K), pos = 1)

knitr::kable(kResult9r$results)


#fit models and effect estimates

summary(out9r)
data_10r <- stm(documents = out9r$documents,
                vocab = out9r$vocab,
                data = out9r$meta,
                prevalence = ~ Negative*Chinese,
                K = 5, verbose = FALSE)
prep_10r <- estimateEffect(1:5 ~ Negative*Chinese, data_10r,
                           meta = out9r$meta)

labelTopics(data_10r, topics = NULL, n = 10)

par(mfrow = c(1, 1),mar = c(7, 1,3, .5))
plot(data_10r, xlim=c(0,.7), n = 7, labeltype = "frex", topic.names = c("(1) President's Response", "(2) Media's response", "(3) States' Response", "(4) Democrat Governors' Response", "(5) Bipartisan Response"), custom.labels = "", text.cex = .7)

summary(prep_10r)

###################### PLOTS #################################


par(mfrow=c(1,2), mai=c(.75,.25,1,.25))
plot(prep_10r, covariate="Chinese", topics=c(5), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     moderator = "Negative", moderator.value = 0,
     xlab="", main="Positively Framed \n Performance Information",cex.main=.70, cex.lab=1.5, cex.axis=0.6,
     xlim=c(-.01,.4), labeltype ="custom", custom.labels=c('Chinese Virus', 'COVID-19'))


plot(prep_10r, covariate="Chinese", topics=c(5), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     moderator = "Negative", moderator.value = 1,
     xlab="", main="Negatively Framed \n Performance Information",cex.main=.70, cex.lab=1.5, cex.axis=0.6,
     xlim=c(-.01,.4), labeltype ="custom", custom.labels=c('Chinese Virus', 'COVID-19'))

mtext("Bipartisan Response \n (B = 0.08, SE = 0.03, p = 0.01)", outer = TRUE, line = -1.8, cex = .8)

###


par(mfrow=c(1,2), mai=c(.75,.25,1,.25))
plot(prep_10r, covariate="Chinese", topics=c(4), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     moderator = "Negative", moderator.value = 0,
     xlab="", main="Positively Framed \n Performance Information",cex.main=.70, cex.lab=1.0, cex.axis=0.6,
     xlim=c(-.01,.3), labeltype ="custom", custom.labels=c('Chinese Virus', 'COVID-19'))


plot(prep_10r, covariate="Chinese", topics=c(4), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     moderator = "Negative", moderator.value = 1,
     xlab="", main="Negatively Framed \n Performance Information",cex.main=.70, cex.lab=1.0, cex.axis=0.6,
     xlim=c(-.01,.3), labeltype ="custom", custom.labels=c('Chinese Virus', 'COVID-19'))

mtext("Democrat Governors' Response \n (B = -0.05, SE = 0.02, p = 0.03", outer = TRUE, side =3, line = -1.8, cex = 0.7)



####


par(mfrow=c(1,2), mai=c(.75,.25,1,.25))
plot(prep_10r, covariate="Chinese", topics=c(3), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     moderator = "Negative", moderator.value = 0,
     xlab="", main="Positively Framed \n Performance Information",cex.main=.70, cex.lab=1.5, cex.axis=0.6,
     xlim=c(-.1,.4), labeltype ="custom", custom.labels=c('Chinese Virus', 'COVID-19'))


plot(prep_10r, covariate="Chinese", topics=c(3), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     moderator = "Negative", moderator.value = 1,
     xlab="", main="Negatively Framed \n Performance Information",cex.main=.70, cex.lab=1.5, cex.axis=0.6,
     xlim=c(-.1,.4), labeltype ="custom", custom.labels=c('Chinese Virus', 'COVID-19'))

mtext("States' Response \n (B = -0.08, SE = 0.03, p = 0.001)", outer = TRUE, line = -1.8, cex = .8)






####################Find thoughts

attach(mydatatreatments_Conservatives)

data9r <- corpus(mydatatreatments_Conservatives, text_field = 'Q22')

docvars(data9r)$text <- texts(data9r)
data9r <- dfm(data9r, stem = TRUE, remove = c(stopwords(source = "smart"),"pandemic", "virus", "respond",  "mask", "wear", "spread"),
              remove_punct = TRUE) %>% dfm_trim(min_termfreq = 25)

out<-convert(data9r, to = 'stm')


model <- stm(out$documents, out$vocab, K = 10, max.em.its = 160, data = out$meta,
             init.type = "Spectral", prevalence =~ Negative*Chinese)

summary(model)


thoughts2<- findThoughts(model, texts = out$meta$Q22, topics = 5, n = 25)

head(thoughts2)

